<?php
namespace Orm\CouponType;

use Base\Exception\RmException;
class CouponTypeClass extends \Base\BaseOrmObject{
    protected   $id,
				$name = '',
				$total_num = 0, // 优惠券剩余数量
				$memo = '', // 备注
				$type = 0, // 1优惠券 2店铺链接
				$link_url = '', // 中奖链接
				$is_del = 0,
				$insert_time = '',
				$update_time = '';

    const STATUS_ONE = 1; // 优惠券
    const LINK_TYPE = 2; // 微商城满118-30全场券
    const LINK_THREE = 3; // 微商城满118-50真空包装产品券

    public static function getStatusList($key = '')
    {
        $list = array(
            self::STATUS_ONE => '优惠券',
            self::LINK_TYPE => '微商城满118-30全场券',
            self::LINK_THREE => '微商城满118-50真空包装产品券',
        );
        if ($key != '') {
            if (isset($list[$key])) {
                return $list[$key];
            } else {
                return '未知';
            }
        }
        return $list;
    }

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\CouponType\CouponTypeModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'name' => $this->name,
			'total_num' => $this->total_num,
			'memo' => $this->memo,
			'type' => $this->type,
			'link_url' => $this->link_url,
        );

        $model = new \Orm\CouponType\CouponTypeModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'name' => $this->name,
			'total_num' => $this->total_num,
			'memo' => $this->memo,
			'type' => $this->type,
			'link_url' => $this->link_url,
        );

        $model = new \Orm\CouponType\CouponTypeModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\CouponType\CouponTypeModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}